#############################################################################
#  Copyright (C) 2007 Boscovich, Maximiliano - mboscovich@gmail.com          #
#                                                                            #
#  This program is free software; you can redistribute it and/or modify      #
#  it under the terms of the GNU General Public License as published by      #
#  the Free Software Foundation; either version 3 of the License, or         #
#  (at your option) any later version. See tht GPL_Licence.txt for details.  #
#                                                                            #
#  This program is distributed in the hope that it will be useful,           #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of            #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              #
#  GNU General Public License for more details.                              #
#                                                                            #
##############################################################################

import sys
sys.path.append("../")

from sqlobject import dberrors
from formencode import Invalid

from view.Office_view import *
from model.tables import *
import logModule


class Office_controller(Ui_Office_view):

    def __init__(self):
        self.widget = QtGui.QDialog()
        self.setupUi(self.widget)
        # Connections
        self.widget.connect(self.pushButtonOk,QtCore.SIGNAL("clicked()"),QtCore.SLOT('close()'))
        self.widget.connect(self.pushButtonAdd,QtCore.SIGNAL("clicked()"),self.addOffice)
        self.widget.connect(self.pushButtonDelete,QtCore.SIGNAL("clicked()"),self.delOffice)
        self.load_list()
    
    # load all registers on listWidget
    def load_list(self):
        self.tableWidgetOffices.clear()
        registers = Office.select()
        for register in registers:
            self.add_new_register_to_table( register )

    def add_new_register_to_table( self,register ):
        # add new row
        self.tableWidgetOffices.setRowCount( self.tableWidgetOffices.rowCount()+1 )
        # Name
        item=QtGui.QTableWidgetItem()
        item.setText( register.Name ) 
        self.tableWidgetOffices.setItem( self.tableWidgetOffices.rowCount()-1, 0, item )
        # Location
        item=QtGui.QTableWidgetItem()
        item.setText( register.Location )
        self.tableWidgetOffices.setItem( self.tableWidgetOffices.rowCount()-1, 1, item )
        # Description
        item=QtGui.QTableWidgetItem()
        item.setText( register.Description )
        self.tableWidgetOffices.setItem( self.tableWidgetOffices.rowCount()-1, 2, item )
        # Contact
        item=QtGui.QTableWidgetItem()
        item.setText( register.Contact )
        self.tableWidgetOffices.setItem( self.tableWidgetOffices.rowCount()-1, 3, item )
        # Phone
        item=QtGui.QTableWidgetItem()
        item.setText( register.Phone )
        self.tableWidgetOffices.setItem( self.tableWidgetOffices.rowCount()-1, 4, item )
    
    def addOffice(self):
        name        = str( self.lineEditName.text() )
        location    = str( self.lineEditLocation.text() )
        description = str( self.lineEditDescription.text() )
        contact     = str( self.lineEditContact.text() )
        phone       = str( self.lineEditPhone.text() )
        try:
            new_office=Office( Name=name, Location=location, Description=description, 
                               Contact=contact, Phone=phone )
            self.add_new_register_to_table( new_office )
        except Invalid,e:
            logModule.log(e)
        except dberrors.DuplicateEntryError:
            logModule.log("La Oficina "+name+" ya existe")
 
    def delOffice(self):
        row_selected = (self.tableWidgetOffices.currentRow())
        office_selected = str( self.tableWidgetOffices.item( row_selected, 0 ).text() )
        self.deleteOffice(office_selected)
        self.tableWidgetOffices.removeRow( row_selected )

    def deleteOffice(self, office):
        office_to_delete=Office.selectBy( Name=office )
        office_to_delete[0].destroySelf()
                
    def editOffice(self, name, location, description, contact, phone):
        pass

